<?php

namespace Alipay\AlipayAuth\Response;

use Alipay\Http\Response;

class AlipayAuthTokenResponse extends Response
{
    private $msg;

    private $sign;

    private $userId;

    private $authAppId;

    private $appAuthToken;

    private $appRefreshToken;

    private $expiresIn;

    private $reExpiresIn;

    public function resolve(): Response
    {
        parent::resolve();

        if (json_decode($this->getBody())) {
            $body = json_decode($this->getBody(), true);
            $this->setBody($body);
            $this->sign = $body['sign'] ?? '';
            $body = $body['alipay_open_auth_token_app_response'] ?? '';
            $code = $body['code'] ?? '';

            if ($code != '10000') {
                $this->setError(trim($this->msg . ' ' . ($body['sub_code'] ?? '') . ' ' . ($body['sub_msg'] ?? '')));
                return  $this;
            }
            $body = $body['tokens'][0];

            $this->userId = $body['user_id'] ?? '';
            $this->authAppId = $body['auth_app_id'] ?? '';
            $this->appAuthToken = $bodyv['app_auth_token'] ?? '';
            $this->appRefreshToken = $body['app_refresh_token'] ?? '';
            $this->expiresIn = $body['expires_in'] ?? '';
            $this->reExpiresIn = $body['re_expires_in'] ?? '';

        } else {
            $this->setError($this->getBody());
        }
        return $this;
    }

    /**
     * @return mixed
     */
    public function getSign()
    {
        return $this->sign;
    }

    /**
     * @return mixed
     */
    public function getUserId()
    {
        return $this->userId;
    }

    /**
     * @return mixed
     */
    public function getAuthAppId()
    {
        return $this->authAppId;
    }

    /**
     * @return mixed
     */
    public function getAppAuthToken()
    {
        return $this->appAuthToken;
    }

    /**
     * @return mixed
     */
    public function getAppRefreshToken()
    {
        return $this->appRefreshToken;
    }

    /**
     * @return mixed
     */
    public function getExpiresIn()
    {
        return $this->expiresIn;
    }

    /**
     * @return mixed
     */
    public function getReExpiresIn()
    {
        return $this->reExpiresIn;
    }


}